/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.transaction.view;

import java.awt.Component;
import java.awt.Dialog.ModalExclusionType;
import java.awt.Window;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import org.balisunrise.annotation.ActionCancel;
import org.balisunrise.annotation.ActionConfirm;
import org.balisunrise.application.Application;
import org.balisunrise.application.BaliProperties;
import org.balisunrise.builder.BuilderException;
import org.balisunrise.common.VisualAction;
import org.balisunrise.common.Grid;
import org.balisunrise.common.EntityActions;
import org.balisunrise.common.EntitySelection;
import org.balisunrise.common.VisualEntity;

/**
 *
 * @author Glauco
 */
public class SelectionDialog extends JDialog implements DialogView{

    private EntitySelection<?,?,JPanel> entitySelection;
    private boolean confirmed = false;

    public SelectionDialog(Window owner, EntitySelection entitySelection) {
        super(owner,ModalityType.APPLICATION_MODAL);
        this.entitySelection = entitySelection;
        this.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
        this.setUndecorated(true);
        init();
    }

    private void init() {

        try{
            VisualAction confirmationAction = Application.get()
                    .getVisualEntityManager()
                    .getVisualActionBuilder()
                    .createConfirmationAction(this, "");

            VisualAction cancellationAction = Application.get()
                    .getVisualEntityManager()
                    .getVisualActionBuilder()
                    .createCancellationAction(this, "");

            EntityActions entityActions = entitySelection.getEntityActions();
            entityActions.setConfirmationAction(confirmationAction);
            entityActions.setCancellationAction(cancellationAction);

            this.add(entitySelection.getVisualRepresentation());
            this.pack();

        }catch(BuilderException bex){
            System.out.println("Falha ao inicial SelectionWindow!\n" +
                    bex.getMessage());
        }
    }

    @Override
    public void setVisible(boolean b) {
        if(b)
            confirmed = false;
        setLocationRelativeTo((Component) Application.get().getVisualRepresentation());

        focusToGrid();

        super.setVisible(b);
    }

    @ActionConfirm(alias="",
        text="PROP:org.balisunrise.text.select",
        toolTipText="PROP:org.balisunrise.tooltiptext.select")
    public void confirm(){

        if(entitySelection.getSelectedEntity() == null){
            JOptionPane.showMessageDialog(this,
                    BaliProperties.getString(BaliProperties.noSelectedEntity),
                    BaliProperties.getString(BaliProperties.selectOneEntity),
                    JOptionPane.INFORMATION_MESSAGE);
            return ;
        }

        confirmed = true;
        setVisible(false);
    }

    @ActionCancel(alias="",
        text="PROP:org.balisunrise.text.cancel",
        toolTipText="PROP:org.balisunrise.tooltiptext.cancel")
    public void cancel(){
        confirmed = false;
        setVisible(false);
    }

    public VisualEntity getVisualEntity() {
        return entitySelection;
    }

    public Boolean getConfirmation() {
        return confirmed;
    }

    private void focusToGrid(){
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                Grid<JTable> grid =
                        entitySelection.getEntityGrid().getGrid();
                grid.getVisualRepresentation().requestFocus();
            }
        });
    }

    public JDialog getVisualRepresentation() {
        return this;
    }
}
